package com.huasoft.servlet.file;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.huasoft.dao.FileDao;
import com.huasoft.dao.UserDao;
import com.huasoft.dao.UserFileDao;
import com.huasoft.domain.File;
import com.huasoft.domain.User;

/**
 * 案卷借阅
 * 
 * @author Administrator
 * 
 */
public class GiveFileServlet {
	public void execute(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		File file = new File();
		FileDao fileDao = new FileDao();
		file = fileDao.findById(Integer.parseInt(request.getParameter("id")));
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		UserDao userDao = new UserDao();
		User managerUser = userDao.findManagerById(user.getId());
		UserFileDao userFileDao = new UserFileDao();
		boolean save = userFileDao.save(user, managerUser, file);
		if (save) {
			if (user.getId() == managerUser.getId()) {
				file.setStatus("已借出");
				fileDao.updateStatus(file);
				request.setAttribute("msg_giveFileSuccess", "您是部门经理，无需审核，借阅成功");
			} else {
				file.setStatus("审核中");
				fileDao.updateStatus(file);
				request.setAttribute("msg_giveFileSuccess", "借阅案卷成功，请等待审核");
			}
		} else {
			request.setAttribute("msg_giveFileFalse", "借阅案卷失败");
		}
		request.getRequestDispatcher("/index.jsp").forward(request, response);
	}
}
